package com.mayihr.fx.attributeBind.highClassBinding;

import com.mayihr.fx.attributeBind.Bill;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.NumberBinding;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;

public class InvalidationListenerTest {
    public static void main(String[] args) {
        Bill bill1 = new Bill();
        Bill bill2 = new Bill();
        Bill bill3 = new Bill();
        NumberBinding total = Bindings.add((bill1.amountDueProperty().add(bill2.amountDueProperty())), bill3.amountDueProperty());

        total.addListener(new InvalidationListener() {
            @Override
            public void invalidated(Observable observable) {
                System.out.println(observable);
                System.out.println("The binding is now invalid.");
            }
        });
        System.out.println("value:"+total.getValue());

        bill1.setAmountDue(200.00);//第一次调用使绑定失效

        bill2.setAmountDue(100.00);//绑定现在无效
        bill3.setAmountDue(75.00);

        //绑定现在有效
        System.out.println("value:"+total.getValue());
        //绑定再此失效
        bill3.setAmountDue(150.00);
        //绑定再此生效
        System.out.println("value:"+total.getValue());
    }
}
